package com.chang.bootplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chang.bootplus.base.exception.BusException;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.BeanUtil;
import com.chang.bootplus.entity.UserLocation;
import com.chang.bootplus.po.*;
import com.chang.bootplus.service.MatrixUserService;
import com.chang.bootplus.service.UserLocationService;
import com.chang.bootplus.vo.LocationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chang
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/location")
@Api(tags = {"收货"})
public class UserLocationController {


    @Autowired
    MatrixUserService matrixUserService;

    @Autowired
    UserLocationService userLocationService;


    //  默认地址施行覆盖形式
    @PostMapping("addLocation")
    @ApiOperation(httpMethod = "POST", value = "增加收货信息")
    public ApiResponse<Void> addLocalMessage(@RequestBody AddLocationPO po){
        UserLocation userLocation = BeanUtil.beanA2beanB(po, UserLocation.class);

        userLocation.setProvince(po.getWideDetails().get(0));
        userLocation.setCity(po.getWideDetails().get(1));
        userLocation.setArea(po.getWideDetails().get(2));
        if(po.getLocationStatus().equals(1)){
            UserLocation one = userLocationService.getOne(new LambdaQueryWrapper<UserLocation>().eq(UserLocation::getUserId, po.getUserId())
                    .eq(UserLocation::getLocationStatus, 1));
            if(one != null){
                one.setLocationStatus(0);
                userLocationService.updateById(one);
            }
        }
        boolean save = userLocationService.save(userLocation);
        if(!save){
            throw new BusException("增加失败");
        }
        return new ApiResponse().setReMsg("增加成功");
    }

    @PostMapping("getDefaultLocation")
    @ApiOperation(httpMethod = "POST", value = "获得默认收货信息")
    public ApiResponse<LocationVO> getDefaultLocation(@RequestBody UserLocationPO po){
        UserLocation one = userLocationService.getOne(
                new LambdaQueryWrapper<UserLocation>().eq(UserLocation::getUserId, po.getUserId()).eq(UserLocation::getLocationStatus,1));

        LocationVO locationVO = BeanUtil.beanA2beanB(one, LocationVO.class);
        ArrayList<String> list = new ArrayList<>();
        list.add(one.getProvince());
        list.add(one.getCity());
        list.add(one.getArea());
        locationVO.setWideDetails(list);
        return new ApiResponse<>(locationVO);
    }

    @PostMapping("listLocationByUserId")
    @ApiOperation(httpMethod = "POST", value = "获得某个人的所有收货信息(数据量小不用分页了吧)")
    public ApiResponse<LocationVO> listLocationByUserId(@RequestBody UserLocationPO po){
        List<UserLocation> list = userLocationService.list(new LambdaQueryWrapper<UserLocation>().eq(UserLocation::getUserId, po.getUserId())
                .orderByDesc(UserLocation::getGmtModified));
        List<LocationVO> locationVOList=new LinkedList<>();
        list.stream().forEach(t->{
            LocationVO locationVO = BeanUtil.beanA2beanB(t, LocationVO.class).setId(t.getId());
            ArrayList<String> details = new ArrayList<>();
            details.add(t.getProvince());
            details.add(t.getCity());
            details.add(t.getArea());
            locationVO.setWideDetails(details);
            locationVOList.add(locationVO);
        });
        return new ApiResponse<>(locationVOList);
    }

    @PostMapping("updateLocation")
    @ApiOperation(httpMethod = "POST", value = "修改收货信息")
    public ApiResponse<Void> updateLocalMessage(@RequestBody UpdateLocationPO po){
        UserLocation userLocation = BeanUtil.beanA2beanB(po, UserLocation.class);
        userLocation.setProvince(po.getWideDetails().get(0));
        userLocation.setCity(po.getWideDetails().get(1));
        userLocation.setArea(po.getWideDetails().get(2));
        if(po.getLocationStatus().equals(1)){
            UserLocation one = userLocationService.getOne(new LambdaQueryWrapper<UserLocation>().eq(UserLocation::getUserId, po.getUserId())
                    .eq(UserLocation::getLocationStatus, 1));
            if(one != null){
                one.setLocationStatus(0);
                userLocationService.updateById(one);
            }
        }
        boolean b = userLocationService.updateById(userLocation);
        if(!b){
            throw new BusException("修改失败");
        }
        return new ApiResponse().setReMsg("修改成功");
    }

    @PostMapping("deleteByLocationId")
    @ApiOperation(httpMethod = "POST", value = "删除收货信息")
    public ApiResponse<Void> deleteLocalMessage(@RequestBody deleteLocationPO po){
        boolean b = userLocationService.removeById(po.getLocationId());
        if (!b){
            throw new BusException("删除失败");
        }
        return new ApiResponse().setReMsg("删除成功");
    }


}

